import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

// HistoryServlet.java 动态数据接口
@WebServlet("/GetHistory")
public class HistoryServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("application/json;charset=UTF-8");

		try {
			List<Feedback> feedbacks = DatabaseUtil.getRecentFeedbacks();

			// 使用Gson转换JSON
			Gson gson = new GsonBuilder()
					.setDateFormat("yyyy-MM-dd HH:mm:ss")
					.create();

			String json = gson.toJson(feedbacks);
			response.getWriter().write(json);

		} catch (SQLException e) {
			e.printStackTrace();
			response.getWriter().write("{\"error\":\"数据库查询失败\"}");
		}
	}
}